package com.xiangban.yanglaodajie_admin.service;

import com.alibaba.fastjson.JSON;
import com.xiangban.yanglaodajie_admin.dao.ClassOrAvtivityExpandMapper;
import com.xiangban.yanglaodajie_admin.entity.ClassMaterials;
import com.xiangban.yanglaodajie_admin.entity.ClassOutline;
import com.xiangban.yanglaodajie_admin.entity.ClassProgress;
import com.xiangban.yanglaodajie_admin.entity.Ids;
import com.xiangban.yanglaodajie_admin.entity.dto.ClassMaterialsDto;
import com.xiangban.yanglaodajie_admin.entity.res.ApiResultCode;
import com.xiangban.yanglaodajie_admin.exception.BizException;
import com.xiangban.yanglaodajie_admin.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

import static com.xiangban.yanglaodajie_admin.utils.CommonUtils.BizEmpty;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/7/30 9:31
 */
@Service
public class ClassOrAvtivityExpandService {

    @Autowired
    private ClassOrAvtivityExpandMapper expandMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private CommonUtils commonUtils;


    /**
     * 添加或修改课程教材
     * @param classMaterialsDto 参数：*，（课程id）
     */
    public void addOrUpdClassMaterials(ClassMaterialsDto classMaterialsDto) {
        if (BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (classMaterialsDto.getId() == null){
            if (BizEmpty(classMaterialsDto.getClassId())){
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            if (BizEmpty(classMaterialsDto.getText())){
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            if (BizEmpty(classMaterialsDto.getImages())){
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            String s = JSON.toJSONString(classMaterialsDto.getImages());
            classMaterialsDto.setImage(s);

            ClassMaterials classMaterials = expandMapper.selectClassMaterials(classMaterialsDto.getClassId());
            if (classMaterials == null){
                expandMapper.insertClassMaterials(classMaterialsDto);
            }else {
                classMaterialsDto.setId(classMaterials.getId());
                expandMapper.updateClassMaterials(classMaterialsDto);
            }

        }else {
            if (classMaterialsDto.getImages().size()>0){
                String s = JSON.toJSONString(classMaterialsDto.getImages());
                classMaterialsDto.setImage(s);
            }
            expandMapper.updateClassMaterials(classMaterialsDto);
        }

    }

    /**
     * 删除课程教材
     * @param classMaterials 参数：教材id
     */
    public void delClassMaterials(ClassMaterials classMaterials) {
        if (BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(classMaterials.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        expandMapper.deleteClassMaterials(classMaterials);
    }

    /**
     * 添加或修改教学大纲
     * @param classOutline 参数：*，（大纲id）
     */
    public void addOrUpdClassOutline(ClassOutline classOutline) {
        if(BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }

        if (classOutline.getId() == null){
            if (BizEmpty(classOutline.getClassId())){
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            if (BizEmpty(classOutline.getText())){
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            if (BizEmpty(classOutline.getSort())){
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            expandMapper.insertClassOutline(classOutline);
        }else {
            expandMapper.updateClassOutline(classOutline);
        }
    }

    /**
     * 可批量删除教学大纲
     * @param ids 参数：商家id、大纲id
     */
    public void delClassOutline(Ids ids) {
        if(BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(ids.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        expandMapper.deleteClassOutline(ids);
    }

    /**
     * 添加或修改教学进度
     * @param classProgress 参数：*，（进度id）
     */
    public void addOrUpdClassProgress(ClassProgress classProgress) {
        if(BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (classProgress.getId() == null){
            Boolean aBoolean = checkRequire(classProgress);
            if (aBoolean){
                expandMapper.insertClassProgress(classProgress);
            }else {
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
        }else {
            expandMapper.updateClassProgress(classProgress);
        }

    }
    private Boolean checkRequire(ClassProgress classProgress){
        if (BizEmpty(classProgress.getClassId())){
            return false;
        }
        if (BizEmpty(classProgress.getTitle())){
            return false;
        }
        if (BizEmpty(classProgress.getOutline())){
            return false;
        }
        if (BizEmpty(classProgress.getStartTime())){
            return false;
        }
        if (BizEmpty(classProgress.getEndTime())){
            return false;
        }
        if (BizEmpty(classProgress.getSort())){
            return false;
        }
        return true;
    }

    /**
     * 可批量删除教学进度
     * @param ids 参数：大纲id
     */
    public void delClassProgress(Ids ids) {
        if(BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(ids.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        expandMapper.deleteClassProgress(ids);
    }
}
